﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using VOD.Models;
using System.Web.Security;
using PagedList;

namespace VOD.Controllers
{ 
    public class CommentController : Controller
    {
        private readonly VODEntities _db = new VODEntities();

        //
        // GET: /Comment/

        public ViewResult Index(int? id,int? page)
        {
            var comments = _db.Comments.OrderByDescending(c=>c.PostTime).ToList();
            if (id != null)
            {
                comments = comments.Where(c => c.VideoID == id).ToList();
            }
            ViewBag.VideoID = id;
            //分页，使用PageList
            const int pageSize = 10; //定义每页显示的记录数
            int pageIndex = (page ?? 1) - 1;
            int pageNumber = page ?? 1;
            return View(comments.ToPagedList(pageNumber, pageSize));
        }

        //
        // GET: /Comment/Details/5

        public ViewResult Details(int id)
        {
            Comment comment = _db.Comments.Find(id);
            return View(comment);
        }

        //
        // GET: /Comment/Create

        public ActionResult Create(int id)
        {
            var comment = new Comment()
            {
                VideoID = id,
            };
            return View(comment);
        } 

        //
        // POST: /Comment/Create

        [HttpPost]
        public ActionResult Create(Comment comment)
        {
            if (ModelState.IsValid) //如果验证通过
            {
                if (HttpContext.Request.IsAuthenticated) //如果用户已经登录
                {
                    //获取当前登录的用户的ID
                    var userID = new Guid(Membership.GetUser().ProviderUserKey.ToString() ?? "");
                    comment.UserID = userID;
                }
                //设置要添加的评论添加时间为当前时间
                comment.PostTime = DateTime.Now;
                _db.Comments.Add(comment);
                //向数据库添加评论
                _db.SaveChanges();
                  
            }
            //返回评论列表
            return RedirectToAction("Index", new { id=comment.VideoID});
        }
        
        //
        // GET: /Comment/Edit/5
 
        public ActionResult Edit(int id)
        {
            Comment comment = _db.Comments.Find(id);
            ViewBag.VideoID = new SelectList(_db.Videos, "VideoID", "Name", comment.VideoID);
            return View(comment);
        }

        //
        // POST: /Comment/Edit/5

        [HttpPost]
        public ActionResult Edit(Comment comment)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(comment).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.VideoID = new SelectList(_db.Videos, "VideoID", "Name", comment.VideoID);
            return View(comment);
        }

        //
        // POST: /Comment/Delete/5

        [HttpPost, ActionName("Delete")]
        [Authorize(Roles="Administrators")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (Request.IsAjaxRequest())
            {
                var comments = _db.Comments.Where(c => c.CommentID == id || c.ParentID == id);
                foreach (var comment in comments)
                {
                    _db.Comments.Remove(comment);
                }
                _db.SaveChanges();
                return Content("succeed");
            }
            return Content("failed");
        }

        protected override void Dispose(bool disposing)
        {
            _db.Dispose();
            base.Dispose(disposing);
        }
    }
}